package cn.edu.ujs.coor.controller;

import cn.edu.ujs.coor.model.Node;
import cn.edu.ujs.coor.request.IdRequest;
import cn.edu.ujs.coor.request.node.NodeAddRequest;
import cn.edu.ujs.coor.request.node.NodeUpdateRequest;
import cn.edu.ujs.coor.response.JsonResponse;
import cn.edu.ujs.coor.service.NodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping(value = "/node")
public class NodeController {

    @Autowired
    private NodeService nodeService;

    @RequestMapping(value = "/list")
    public JsonResponse list () {
        List<Node> list = nodeService.findAll();
        return JsonResponse.success(list);
    }

    @RequestMapping(value = "/add")
    public JsonResponse create (@RequestBody @Valid NodeAddRequest request) {
        nodeService.save(request.convertToEntity(Node.class));
        return JsonResponse.success();
    }

    @RequestMapping(value = "/update")
    public JsonResponse update (@RequestBody @Valid NodeUpdateRequest request) {
        nodeService.update(request);
        return JsonResponse.success();
    }

    @RequestMapping(value = "/delete")
    public JsonResponse delete (@RequestBody @Valid IdRequest request) {
        nodeService.delete(request);
        return JsonResponse.success();
    }
}
